/**********************************************************************
 * SailmiPIM - modular cross-platform feature rich internet client.
 * Copyright (C) 2006-2014  Georg Rudoy
 *
 * Distributed under the Boost Software License, Version 1.0.
 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
 **********************************************************************/

#include "menucomponent.h"
#include <QMenu>
#include <QQuickImageProvider>
#include <util/sys/paths.h>
#include <util/core/util.h>
#include <sailmipim/imwproxy.h>

namespace SailmiPIM::Plugin::QuickBar
{
	namespace
	{
        class MenuImageProvider final : public QQuickImageProvider
		{
		public:
            MenuImageProvider ()
			: QQuickImageProvider (Pixmap)
			{
			}

            MenuImageProvider (const MenuImageProvider&) = delete;
            MenuImageProvider (MenuImageProvider&&) = delete;

			QPixmap requestPixmap (const QString&, QSize*, const QSize&)
			{
                return QPixmap { QStringLiteral ("smicons:/resources/images/sailmipim.svg") };
			}
		};

        const QString ImageProviderID = QStringLiteral ("MenuImage");
	}

    MenuComponent::MenuComponent (IMWProxy *proxy, QObject *parent)
	: QObject (parent)
	, Proxy_ (proxy)
    , Component_ (std::make_shared<QuarkComponent> ("QuickBar", "MenuComponent.qml"))
	{
		Component_->DynamicProps_.append ({ QStringLiteral ("menuComponentProxy"), this });
        Component_->StaticProps_.append ({ QStringLiteral ("menuComponentIcon"), "image://" + ImageProviderID + "/icon" });
        Component_->StaticProps_.append ({ QStringLiteral ("menuTooltipString"), tr ("sailmipim menu") });
        Component_->ImageProviders_.append ({ ImageProviderID, new MenuImageProvider });

		Proxy_->HideMainMenu ();
	}

    QuarkComponent_ptr MenuComponent::GetComponent () const
	{
		return Component_;
	}

    void MenuComponent::execMenu ()
	{
		Proxy_->GetMainMenu ()->exec (QCursor::pos ());
	}
}
